package igrupobbva.pe.sistemareniec.actions;


import igrupobbva.pe.sistemareniec.forms.MenuForm;
import igrupobbva.pe.sistemareniec.forms.PerfilForm;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import proxy.load.Connection;

import com.grupobbva.iici.serializable.Corporativa;
import com.grupobbva.iici.serializable.Menu;
import com.grupobbva.iici.serializable.Perfil;
import com.grupobbva.iici.util.ConstantesUtil;

public class CorporativaAction extends DispatchAction {
	static Logger logger = Logger.getLogger(CorporativaAction.class.getName());
	private List listaUsuarios; 
	

	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
request.getSession().removeAttribute("listaCorporativa");
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
		Connection conexion = new Connection();
		try { 			
			request.getSession().setAttribute("listaCorporativa", conexion.findByCodMulti("T01"));
			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);

	}
	public ActionForward editar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value		
		Connection conexion = new Connection();
		Vector listMenu = null;
		String codMult = (String) request.getParameter("codMult");
		String codElem = (String) request.getParameter("codElem");
		String valor1 = (String) request.getParameter("valor1");
		ArrayList collecUsuario=new ArrayList();
		ArrayList listaTitulo = new ArrayList();
		
		try { 
			Corporativa corporativa = new Corporativa();
			corporativa.setCodMult(codMult);
			corporativa.setCodEle(codElem);
			corporativa.setValor1(valor1.trim());		
			conexion.updateCorporativa(corporativa);
			request.getSession().setAttribute("listaCorporativa", conexion.findByCodMulti("T01"));			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);	

	}
	public ActionForward nuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
	
		Connection conexion = new Connection();
		Vector listMenu = null;
		String padre = (String) request.getParameter("padre");
		String hijo = (String) request.getParameter("hijo");
		String descripcion = (String) request.getParameter("descripcion");
		String codPerfil = (String) request.getParameter("codPerfil");
		String link = (String) request.getParameter("link");
		
		ArrayList collecUsuario=new ArrayList();
		ArrayList listaTitulo = new ArrayList();
		
		try { 
			Menu menu = new Menu();
			menu.setPadre(padre);
			menu.setHijo(hijo);
			
			menu.setCodPerfil(codPerfil);
			menu.setDescripcion(descripcion);
			menu.setLink(link);
			conexion.createMenu(menu);		
			request.getSession().setAttribute("listaMenu", conexion.findAllMenu());		
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);	

	}
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward();		
		Connection conexion = new Connection();
		Vector listMenu = null; 
		String padre = (String) request.getParameter("padre");	
		try {			
			String temp[]=padre.split(ConstantesUtil.SEPARADOR_CODIGOS);		
			for(int i=0;i<temp.length;i++){		
			conexion.deleteMenu(new BigDecimal(String.valueOf(temp[i])));
			}			
			request.getSession().setAttribute("listaMenu", conexion.findAllMenu());			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		return (forward);	

	}
	public ActionForward mostrarEditar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
		PerfilForm perfilForm = (PerfilForm) form;
		Connection conexion = new Connection();
		Vector listMenu = null;	
		String codPerfil = (String) request.getParameter("codPerfil");
		try { 
			Perfil perfil = conexion.findPerfil(codPerfil);
			perfilForm.setCodPerfil(perfil.getCodPerfil());
			perfilForm.setDesPerfil(perfil.getDesPerfil());		
		
			
			request.setAttribute("title", "Editar");		
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		// Finish with
		return (forward);	

	}
	
	public ActionForward mostrarNuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
		MenuForm perfilForm = (MenuForm) form;
		Connection conexion = new Connection();
		Vector listMenu = null;			
		try { 
		
			perfilForm.setCodPerfil("");
			perfilForm.setCodPerfil("");			
			request.getSession().setAttribute("title", "Nuevo");		
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		// Finish with
		return (forward);	

	}
	/**
	 * @return Returns the listaUsuarios.
	 */
	public List getListaUsuarios() {
		return listaUsuarios;
	}
	/**
	 * @param listaUsuarios The listaUsuarios to set.
	 */
	public void setListaUsuarios(List listaUsuarios) {
		this.listaUsuarios = listaUsuarios;
	}
	
	
	
	
}
